function load(){
	addClickEventForLeftOption();
	addClickEventForRightOption();
	if($('#firstLineAbled').val()=="true"){
		$('#firstAbled').attr("checked",true);
	}
}

var selectedFlag = false;

function addClickEventForLeftOption(){
	$('.leftOption').each(function(){
		$(this).click(function(){
			leftClickFuntion(this);
		});
	})
}


function leftClickFuntion(option){
	$('.rFstOpt').each(function(){
		$(option).removeClass('rOptSelected');
	});
	$('.rSecOpt').each(function(){
		$(option).removeClass('rOptSelected');
	});
	
	$temp = $(option).parent();
	if(!$temp.is("ul")){
		$temp = $temp.parent();
	}
	$tr = $temp;
	$tr.children().each(function(){
		$(this).children().each(function(){
			$(this).removeClass('lOptSelected');
		});
	});
	$(option).addClass('lOptSelected');

}

function rightFstClickFunction(option){
	$('.leftOption').each(function(){
		$(this).removeClass('lOptSelected');
	})
	
	$temp = $(option).parent();
	if(!$temp.is("ul")){
		$temp = $temp.parent();
	}
	$tr = $temp;
	$tr.children().each(function(){
		$(this).children().each(function(){
			$(this).removeClass('rOptSelected');
			$(this).next('div').removeClass('rOptSelected');
		});
	});
	$(option).addClass('rOptSelected');
	$(option).next('div').addClass('rOptSelected');

}

function rightSecClickFunction(option){
	$temp = $(option).parent();
	if(!$temp.is("ul")){
		$temp = $temp.parent();
	}
	$tr = $temp;
	$tr.children().each(function(){
		$(this).children().each(function(){
			$(this).removeClass('rOptSelected');
			$(this).prev('div').removeClass('rOptSelected');
		});
	});
	$(option).addClass('rOptSelected');
	$(option).prev('div').addClass('rOptSelected');
}

function addClickEventForRightOption(){
	$('.rFstOpt').each(function(){
		$(this).click(function(){
			rightFstClickFunction(this);
		});
	});
	
	$('.rSecOpt').each(function(){
		$(this).click(function(){
			rightSecClickFunction(this);
		});
	});
}

function addClickEventForSave(){
	$('.saveClass').each(function(){
		$(this).click(function(){
			var $temp = $(this).parent();
			while(!$temp.is('tr')){
				$temp = $temp.parent();
			}
			var $tr = $temp;
			var type = $('#type').val();
			var array = $tr.children();
			var id = $(array[0]).text()
			var innerId = $(array[1]).text();
			var innerName = $(array[2]).text();
			var outId = $($(array[3]).children()[0]).val();
			var outName = $($(array[4]).children()[0]).val();
			var operation = $($(array[5]).children()[0]).val();
			var state = $(array[6]).children()[0].checked;
			var objectType = $($(array[7]).children()[0]).val();
			state = state ? 1 : 0;
			$.ajax({
				url:"saveOrUpdate",
				type:"post",
				dataType:"json",
				async:true,
		        data: { //发送给数据库的数据
					id : id,
				 	type : type,
				 	innerId : innerId,
				 	innerName : innerName,
				 	outId : outId,
				 	outName : outName,
				 	operation : operation,
				 	objectType : objectType,
				 	state : state
				 },
				success:function(msg){
					 alert("保存数据成功")
				 },
				error: function (XMLHttpRequest, textStatus, thrownError) {
					 alert("保存数据失败")
				 }
			
			});
		});
	});
}

function back(){
	window.location.href = "dataTransition";
}

function saveAll(){
	var checkAll = $('#checkAll').attr('checked');
	var flag = true;
	if(!checkAll){
		$('.checkState').each(function(){


			var $temp = $(this).parent();
			while(!$temp.is('tr')){
				$temp = $temp.parent();
			}
			var $tr = $temp;
			var type = $('#type').val();
			var array = $tr.children();
			var id = $(array[0]).text()
			var innerId = $(array[1]).text();
			var innerName = $(array[2]).text();
			var outId = $($(array[3]).children()[0]).val();
			var outName = $($(array[4]).children()[0]).val();
			var operation = $($(array[5]).children()[0]).val();
			var state = $(array[6]).children()[0].checked;
			var objectType = $($(array[7]).children()[0]).val();
			state = state ? 1 : 0;
			$.ajax({
				url:"saveOrUpdate",
				type:"post",
				dataType:"json",
				async:true,
		        data: { //发送给数据库的数据
					id : id,
				 	type : type,
				 	innerId : innerId,
				 	innerName : innerName,
				 	outId : outId,
				 	outName : outName,
				 	operation : operation,
				 	objectType : objectType,
				 	state : state
				 },
				success:function(msg){
				 },
				error: function (XMLHttpRequest, textStatus, thrownError) {
					 flag = false;
				 }
			
			});
		
		});
		
		if(flag){
			alert("保存成功");
		}
	}
}

function toRight(){
	var $leftUl = $('#leftUl');
	var txt;
	var dataType;
	$leftUl.children().each(function(){
		$li = $(this);
		$li.children().each(function(){
			$div = $(this);
			if($div.hasClass('lOptSelected')){
				txt = $div.text().trim();
				dataType = $div.attr("value").trim();
				$li.remove();
				var $rightUl = $('#rightUl');
				var li = "<li><div class='rFstOpt'>"+txt+"</div><div class='rSecOpt' value='"+dataType+"'><input type='text' style='width:50%' value='"+txt+"'></input></div></li>";
				$rightUl.append(li);
				addClickEventForRightOption();
			}
		});
	});
}

function toLeft(){
	var $rightUl = $('#rightUl');
	var txt;
	var dataType;
	$rightUl.children().each(function(){
		$li = $(this);
		$li.children().each(function(){
			$div = $(this);
			if($div.hasClass('rOptSelected')){
				txt = $div.text().trim();
				dataType = $div.next('div').attr("value").trim();
				$li.remove();
				var $leftUl = $('#leftUl');
				var div = "<div class='leftOption' value='"+dataType+"'>"+txt+"</div>";
				var li = "<li>"+div+"</li>";
				$leftUl.append(li);
				addClickEventForLeftOption();
			}
		});
	});
}


function up(){
	var changeFlag = false;
	var $rightUl = $('#rightUl');
	$rightUl.children().each(function(){
		$li = $(this);
		$li.children().each(function(){
			if(!changeFlag){
				$div = $(this);
				if($div.hasClass('rOptSelected')){
					$upDiv = $li.prev();
					$li.insertBefore($upDiv);
					changeFlag = true;
				}
			}
		});
	});
}


function down(){
	var changeFlag = false;
	var $rightUl = $('#rightUl');
	$rightUl.children().each(function(){
		$li = $(this);
		$li.children().each(function(){
			if(!changeFlag){
				$div = $(this);
				if($div.hasClass('rOptSelected')){
					$downDiv = $li.next();
					$li.insertAfter($downDiv);
					changeFlag = true;
				}
			}
		});
	});
}

function downloadTemplate(){
	var character = "utf-8";
	var mb = myBrowser();
	if ("FF" == mb) {
	   character =  "GB2312"
	}
	var today = timeFormat(new Date())
	var fileName = encodeURI("PosImportTemplate_"+today);
	var dataStr = "";
	$('.rSecOpt').each(function(){
		var input = $(this).children()[0];
		var columnName = $(input).val().trim();
		var dataType = $(this).attr('value').trim();
		dataStr+="{\"columnName\":\""+columnName+"\",\"dataType\":\""+dataType+"\"},";
	});
	dataStr = "["+dataStr.substring(0, dataStr.length-1)+"]";
	dataStr = encodeURI(dataStr);
	$("#template").children("input[name='dataStr']").val(dataStr);
	$("#template").children("input[name='fileName']").val(fileName);
	$("#template").children("input[name='character']").val(character);
	$("#template").submit();
}

function save(){
	var typeId = $('#type').val().trim();
	var firstLineAbled = $('#firstAbled').attr('checked');
	firstLineAlbled = firstLineAbled == true ? 1 : 0;
	var tableName = $('#tableName').val().trim();
	var columnOrder=0;
	var dataStr = "";
	if($('.rFstOpt').length==0){
		dataStr += "{\"typeId\":\""+typeId+"\",\"columnName\":\"\",\"columnAlias\":\"\""
		+ ",\"columnOrder\":\"\",\"dataType\":\"\",\"firstLineAbled\":\""+firstLineAbled+"\""
		+ ",\"tableName\":\""+tableName+"\"},";
	}
	$('.rFstOpt').each(function(){
		var columnName = $(this).text().trim();
		var $rSecDiv = $(this).next();
		var dataType = $rSecDiv.attr('value');
		var input = $rSecDiv.children()[0];
		var columnAlias = $(input).val().trim();
		dataStr += "{\"typeId\":\""+typeId+"\",\"columnName\":\""+columnName+"\",\"columnAlias\":\""+columnAlias+"\""
				+ ",\"columnOrder\":\""+columnOrder+"\",\"dataType\":\""+dataType+"\",\"firstLineAbled\":\""+firstLineAbled+"\""
				+ ",\"tableName\":\""+tableName+"\"},";
		columnOrder++;
	});
	dataStr = "["+dataStr.substring(0, dataStr.length-1)+"]";
	dataStr = encodeURI(dataStr);
	$.ajax({
			type: "GET",
			contentType: "application\json",
			dataType: "json",
			data:{
				dataStr: dataStr
			},
			url: "saveTemplate",
			async:true,
			success: function(msgs){
				alert("模板保存成功");
			}		
	});
}

/*
格式化称yyyymtddhhmmss
*/
function timeFormat(date){
	var year = date.getFullYear();
	var month = (date.getMonth()+1);
	var day = date.getDate();
	var hour = date.getHours();
	var minutes = date.getMinutes();
	var seconds = date.getSeconds();
	if (month<10) {
		month = '0'+month
	} else {
		month = ''+month
	}
	if (day<10) {
		day = '0'+day
	} else {
		day = ''+day
	}
	if (hour<10) {
		hour = '0'+hour
	} else {
		hour = ''+hour
	}
	if (minutes<10) {
		minutes = '0'+minutes
	} else {
		minutes = ''+minutes
	}
	if (seconds<10) {
		seconds = '0'+seconds
	} else {
		seconds = ''+seconds
	}
	return year+month+day+hour+minutes+seconds
} 

function myBrowser(){
    var userAgent = navigator.userAgent; //取得浏览器的userAgent字符串
    var isOpera = userAgent.indexOf("Opera") > -1;
    if (isOpera) {
        return "Opera"
    }; //判断是否Opera浏览器
    if (userAgent.indexOf("Firefox") > -1) {
        return "FF";
    } //判断是否Firefox浏览器
    if (userAgent.indexOf("Chrome") > -1){
  return "Chrome";
 }
    if (userAgent.indexOf("Safari") > -1) {
        return "Safari";
    } //判断是否Safari浏览器
    if (userAgent.indexOf("compatible") > -1 && userAgent.indexOf("MSIE") > -1 && !isOpera) {
        return "IE";
    }; //判断是否IE浏览器
} 